package com.ruoyi.web.controller.vantController.paymentAlipayController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.construction.strategy.ConstructionStrategy;
import com.ruoyi.system.domain.customer.PayableInstalment;
import com.ruoyi.system.domain.zhuangxiumain.orderformMain.OrderMaster;
import com.ruoyi.system.service.payment.WxAppTestPayService;
import com.ruoyi.system.service.payment.WxShareService;
import com.ruoyi.web.controller.payment.WeixinshareTwoController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;

/**
 * 微信支付接口
 */
@CrossOrigin
@RestController
@RequestMapping("/api/wxPayTest")
public class ApiWxPayTestController extends BaseController {

    @Autowired
    private WxAppTestPayService wxService;

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private WxShareService wxShareService;
    @Autowired
    private HttpServletResponse response;
    /**
     * APP支付
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/orders_other")
    public AjaxResult wxAdd(@RequestBody OrderMaster orderMaster) throws Exception {
        int a = (int) (orderMaster.getPaymentMoney()*100);
        String price = String.valueOf(a);
        return AjaxResult.success(wxService.doUnifiedOrder(request,price));
    }

    @PostMapping(value = "/ordersOtherTwo")
    public AjaxResult wxAddTwo(@RequestBody PayableInstalment payableInstalment) throws Exception {
        Integer paymentOne = payableInstalment.getPaymentOne();
        System.err.println("+***paymentOne****"+paymentOne);
        String price = String.valueOf(paymentOne);
        return AjaxResult.success(wxService.doUnifiedOrder(request,price));

    }

    @GetMapping(value = "/getSignature")
    public AjaxResult getSignature() throws IOException, ParseException {
        return AjaxResult.success(wxShareService.getSignature(request,response));
    }

    /**
     * 微信支付回调
     * @param request
     * @return
     */
    @PostMapping("/wxnotify")
    @ApiOperation("微信回调")
    public String wxPayNotify(HttpServletRequest request) {
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                System.out.println(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = wxService.payBack(resXml);
            return result;
        } catch (Exception e) {
            System.out.println("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            return result;
        }
    }
}
